//
//  HZJImageViewController.swift
//  HZJTextView
//
//  Created by HZJ on 17/1/11.
//  Copyright © 2017年 HZJ. All rights reserved.
//

import UIKit

class HZJImageViewController: UIViewController {

    var theImage:UIImage?
    weak var delegate:HZJImageViewControllerDelegate?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = UIColor.white
        imageV.frame = view.frame
        self.view.addSubview(imageV)
    }
    override func viewWillAppear(_ animated: Bool) {
        self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
//        self.navigationController?.navigationBar.shadowImage = UIImage()
        
        let leftBtn = UIButton()
        leftBtn.frame = CGRect(x: 0, y: 0, width: 80, height: 40)
        leftBtn.setTitleColor(UIColor.randomColor(), for: UIControl.State())
        leftBtn.setTitle("CANCEL", for: UIControl.State())
        leftBtn.addTarget(self, action: #selector(HZJImageViewController.clickTheLeftBtn), for: UIControl.Event.touchUpInside)
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: leftBtn)
        
        let rightBtn = UIButton()
        rightBtn.frame = CGRect(x: 0, y: 0, width: 80, height: 40)
        rightBtn.setTitleColor(UIColor.randomColor(), for: UIControl.State())
        rightBtn.setTitle("SAVE", for: UIControl.State())
        rightBtn.addTarget(self, action: #selector(HZJImageViewController.clickTheRightBtn), for: UIControl.Event.touchUpInside)
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: rightBtn)
        
    }
    @objc func clickTheLeftBtn() {
        self.dismiss(animated: true, completion: nil)
    }
    
    @objc func clickTheRightBtn(){
        self.theImage = imageV.getImage()
        self.delegate?.getImage!(theImage)
        self.dismiss(animated: true, completion: nil)
    }
        
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    fileprivate lazy var imageV:HZJImageView = HZJImageView()
}

@objc protocol HZJImageViewControllerDelegate :NSObjectProtocol {
    @objc optional func getImage(_ image:UIImage?)
}
